package jsj.myjdbc_01;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;

public class TransactionDemo {
	

	public static void main(String[] args) {
		String insertSql1 = "insert into student values('95011','zhangsan',25,'male')";
		String insertSql2 = "insert into student values('95012','lisi',25,'male')";
		String insertSql3 = "insert into student values('95011','wangwu',25,'male')";
		Savepoint s1 = null;
		
		Connection conn = DBUtil.getConnection();
		try {
			Statement stmt = conn.createStatement();
			boolean autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);
			
			stmt.execute(insertSql1);
			s1 = conn.setSavepoint();
			stmt.execute(insertSql2);
//			stmt.execute(insertSql3);
			if(true) {
				conn.rollback(s1);
			}
			
			conn.commit();
			conn.setAutoCommit(autoCommit);
			
		} catch (Exception e) {
			if(conn != null) {
				try {
					conn.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			e.printStackTrace();
		}

	}

}
